package com.chengyu.core.controller.callback.logic;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.chengyu.core.controller.callback.PayCallbackController;
import com.chengyu.core.controller.callback.manager.CallbackFactory;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.enums.AccountEnums;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.CommonPayOrderMapper;
import com.chengyu.core.model.CommonPayOrder;
import com.chengyu.core.model.CommonPayOrderExample;
import com.chengyu.core.model.UmsMember;
import com.chengyu.core.model.UmsMemberRecharge;
import com.chengyu.core.service.funds.MemberAccountLogService;
import com.chengyu.core.service.member.MemberRechargeService;
import com.chengyu.core.service.member.MemberService;
import com.chengyu.core.service.order.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @title  充值成功
 * @author LeGreen
 * @date   2022/6/9
 */
@Service("rechargeLogic")
public class RechargeLogic extends CallbackFactory {

	private Logger logger = LoggerFactory.getLogger(RechargeLogic.class);

	@Autowired
	private MemberRechargeService rechargeService;

	@Autowired
	private CommonPayOrderMapper payOrderMapper;


	@Autowired
	private MemberAccountLogService memberAccountLogService;


	@Autowired
	private MemberService memberService;


	@Override
	public void paySus(String orderNo) throws ServiceException {
		UmsMemberRecharge recharge = rechargeService.getRechargeLogByOrderNo(orderNo);


		CommonPayOrderExample payOrderExample = new CommonPayOrderExample();
//		payOrderExample.createCriteria().andPayOrderNoEqualTo(orderNo).andStatusEqualTo(CommonConstant.WAIT_INT);
		payOrderExample.createCriteria().andOrderSnEqualTo(orderNo).andStatusEqualTo(CommonConstant.WAIT_INT);
		List<CommonPayOrder> payOrderList = payOrderMapper.selectByExample(payOrderExample);
		if(CollectionUtil.isNotEmpty(payOrderList)) {
			CommonPayOrder updatePayOrder = new CommonPayOrder();
			updatePayOrder.setId(payOrderList.get(0).getId());
			updatePayOrder.setStatus(CommonConstant.SUS_INT);
			updatePayOrder.setUpdTime(DateUtil.date());
			updatePayOrder.setPayTime(updatePayOrder.getUpdTime());
			payOrderMapper.updateByPrimaryKeySelective(updatePayOrder);

			//支付成功, 更新状态
			logger.info("recharge.getStatus():{}",recharge.getStatus());
			logger.info("CommonConstant.WAIT:{}",CommonConstant.WAIT);
			if(Objects.equals(recharge.getStatus(), CommonConstant.WAIT)){
//				logger.info("解密后的明文:{}",plainBody);
				if(recharge.getType().equals(AccountEnums.AccountMethod.BALANCE.getValue())){
					rechargeService.rechargSusByCallback(recharge);
				}else 	if(recharge.getType().equals(AccountEnums.AccountMethod.BZJ.getValue())){
					rechargeService.rechargBzjSusByCallback(recharge);
				}
			}else{

			}
//			logger.info("解密后的明文:{}",plainBody);

		}


	}

	@Override
	public void refundSus(String orderNo) {
		throw new UnsupportedOperationException();
	}

}
